﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SweepOfCC
{
    /// <summary>
    /// 按照步骤 1a 或 1b 操作，然后执行步骤 2 以在 XAML 文件中使用此自定义控件。
    ///
    /// 步骤 1a) 在当前项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:SweepOfCC"
    ///
    ///
    /// 步骤 1b) 在其他项目中存在的 XAML 文件中使用该自定义控件。
    /// 将此 XmlNamespace 特性添加到要使用该特性的标记文件的根 
    /// 元素中: 
    ///
    ///     xmlns:MyNamespace="clr-namespace:SweepOfCC;assembly=SweepOfCC"
    ///
    /// 您还需要添加一个从 XAML 文件所在的项目到此项目的项目引用，
    /// 并重新生成以避免编译错误: 
    ///
    ///     在解决方案资源管理器中右击目标项目，然后依次单击
    ///     “添加引用”->“项目”->[浏览查找并选择此项目]
    ///
    ///
    /// 步骤 2)
    /// 继续操作并在 XAML 文件中使用控件。
    ///
    ///     <MyNamespace:BoomControl/>
    ///
    /// </summary>
    public class BoomControl : ContentControl
    {
        static BoomControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BoomControl), new FrameworkPropertyMetadata(typeof(BoomControl)));
        }

        #region 依赖属性

        public BoomControlState Status
        {
            get { return (BoomControlState)GetValue(StatusProperty); }
            set { SetValue(StatusProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Status.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StatusProperty =
            DependencyProperty.Register("Status", typeof(BoomControlState), typeof(BoomControl), new PropertyMetadata(BoomControlState.Heddon, new PropertyChangedCallback(OnStatusChanged)));

        private static void OnStatusChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var bc = sender as BoomControl;
            if (bc == null || bc.BoomInfo == null)
                return;

            switch (bc.Status)
            {
                case BoomControlState.Heddon:
                    bc.Content = string.Empty;
                    break;
                case BoomControlState.Flaged:
                    bc.Content = "F";
                    break;
                case BoomControlState.Quary:
                    bc.Content = "?";
                    break;
                case BoomControlState.Discovered:
                    bc.Content = bc.BoomInfo.AroundCount;
                    break;
                default:
                    break;
            }
        }



        public BoomInformation BoomInfo
        {
            get { return (BoomInformation)GetValue(BoomInfoProperty); }
            set { SetValue(BoomInfoProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BoomInfo.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BoomInfoProperty =
            DependencyProperty.Register("BoomInfo", typeof(BoomInformation), typeof(BoomControl), new PropertyMetadata(null, new PropertyChangedCallback(OnBoomInfoChanged)));

        private static void OnBoomInfoChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var bc = sender as BoomControl;
            if (bc == null)
                return;

            if (bc.BoomInfo == null)
                bc.IsBoom = false;
            else
                bc.IsBoom = bc.BoomInfo.IsBoom;
        }

        public bool IsBoom
        {
            get { return (bool)GetValue(IsBoomProperty); }
            set { SetValue(IsBoomProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsBoom.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsBoomProperty =
            DependencyProperty.Register("IsBoom", typeof(bool), typeof(BoomControl));


        #endregion


        private void Discover()
        {
            this.Status = BoomControlState.Discovered;
        }
    }
}
